using System;
using System.Threading;

namespace codebase.UI
{
    public class ScrollingGraph : Control
    {
        public delegate int DataGrabberDelegate();

        public int BarColor;
        public int MaxValue = 0;
        public bool AutoMax = true;

        Timer _timer;

        int NewIndex;
        int BarCount;
        int BarWidth;
        int[] BarValues;

        public DataGrabberDelegate DataGrabberFunction;

        public ScrollingGraph(int x, int y, int w, int h, int bgc, int bc, int interval, Window parent)
        {
            Init(x, y, w, h, bgc, bc, interval,parent);
        }

        public void Stop()
        {
            _timer.Dispose();
            _timer = null;
        }

        public void Start(int interval)
        {
            _timer = new Timer(new TimerCallback(Refresh), null, interval, interval);
        }

        void Init(int x, int y, int w, int h, int bgc, int bc, int interval, Window parent)
        {
            Parent = parent;

            Left = x * Parent.Parent.screen.CharacterWidth;
            Top = y * Parent.Parent.screen.CharacterHeight;
            Width = w * Parent.Parent.screen.CharacterWidth;
            Height = h * Parent.Parent.screen.CharacterHeight;

            BackgroundColor = bgc;
            BarColor = bc;

#if MINI
            BarWidth = Parent.Parent.screen.CharacterWidth;
#else
            BarWidth = Parent.Parent.screen.CharacterWidth / 2;
#endif
            BarCount = (Width * Parent.Parent.screen.CharacterWidth) / BarWidth;

            BarValues = new int[BarCount];
            NewIndex = 0;

            Start(interval);
        }

        void Clear()
        {
            Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left, Top + Parent.Top, Left + Parent.Left + (BarCount * BarWidth), Top + Parent.Top + Height - 1, BackgroundColor);
        }

        void Refresh(Object o)
        {
            int value = DataGrabberFunction();
            BarValues[NewIndex] = value;
            NewIndex = (NewIndex + 1) % BarCount;

            if (AutoMax)
            {
                MaxValue = 0;
                foreach (int v in BarValues)
                {
                    if (v > MaxValue) MaxValue = v;
                }
            }
        }

        protected override void DrawAll()
        {
            Draw();
        }

        protected override void Draw()
        {
            int i;
            int value;
            int id;

            Clear();

            //Parent.Parent.screen.SPE.OutlineColor(ColorExtensions.RGB16(100, 200, 50));

            Parent.Parent.screen.DrawRectangle(Left + Parent.Left, Top + Parent.Top, Left + Parent.Left + Width, Top + Parent.Top + Height, ColorExtensions.RGB16(100, 200, 50));

            for (i = 0; i < BarCount; i++)
            {
                id = (NewIndex + i) % BarCount;
                if (BarValues[id] == 0)
                {
                    value = 0;
                }
                else
                {
                    value = (int)((float)(Height - 1) * ((float)(BarValues[id]) / (float)MaxValue));

                    int _x, _y;
                    _x = Left + Parent.Left + (i * BarWidth);
                    _y = Top + Parent.Top + Height - 1;

                    Parent.Parent.screen.DrawFilledRectangle(_x, _y - value, _x + BarWidth, _y, BarColor);
                }
            }
            
            //Parent.Parent.screen.SPE.OutlineColor(0);
        }
    }

}
